Ext.onReady(function(){
	FlowManager.initPage();
});

var FlowManager = {
		initPage: function(){
			Ext.create('Ext.container.Viewport', {
			    layout: 'border',
			    frame:false,
			    border:false,
			    items: [{
			    	id: 'mainPanel',
			        region: 'center',
			        layout: 'fit',
			        items:[FlowManager.createFlowGrid()]
			    }]
			});
		},
		createFlowGrid: function(){
			var grid = Ext.create('Ext.com.bkfm.datagrid', {
				id: "flowGrid",
				objPath : "ROOT/WORKFLOW/LCGL/BLK_WORKFLOW",
				hiddenField: ['WORKFLOW_CONFIG','IS_DEPLOYED'],
				formUnViewField: ["WORKFLOW_CONFIG","IS_DEPLOYED"],
				userConfigColumn:[{
					name: 'DEPLOYED',
					text: '是否已部署',
					dataIndex: 'DEPLOYED',
					renderer: function(value, cellmeta, record, rowIndex, columnIndex, store){
						if(Ext.isEmpty(record.get("IS_DEPLOYED")) || Constants.NO == record.get("IS_DEPLOYED")){
							return "未部署";
						}else{
							return "已部署";
						}
					}
				},{
					name: 'WORKFLOW_CONFIG_JSON',
					text: '流程配置状态',
					dataIndex: 'WORKFLOW_CONFIG_JSON',
					renderer: function(value, cellmeta, record, rowIndex, columnIndex, store){
						if(Ext.isEmpty(record.get("WORKFLOW_CONFIG"))){
							return "未配置";
						}else{
							return "已配置";
						}
					}
				}],
				border: true,
				customBtn:[{
					text: '流程设计',
					iconCls: "flow",
					handler: function(){
						var record = grid.getSelectedRecord();
						if(record==null){
							return;
						}
						var flowWin = 	Ext.create('Ext.com.bkfm.window', {
							title : "流程图设计器",
							id: 'flowWin',
							autoScroll : true,
							modal : true,
							height : Utils.getHeight()*0.8,
							width : Utils.getWidth()*0.7,
							maximized: true,
							closable: false,
							html: '<iframe style="width:100%;height:100%;" frameborder="0" src="'+CONTEXT.contextPath+'/web/flow/designer/designer.jsp?WORKFLOW_CODE='+record.get("WORKFLOW_CODE")+'&WORKFLOW_NAME='+record.get("WORKFLOW_NAME")+'"></iframe>'
						});
						flowWin.show();
					}
				},{
					text: '部署流程',
					iconCls: 'deployflow',
					handler: function(self){
						var selfGrid = self.ownerCt.ownerCt;
						var record = selfGrid.getSelectedRecord();
						if(!record){
							AjaxUtils.showError("请选择一条需要部署的流程记录");
							return;
						}
						if(record.get("IS_DEPLOYED") == Constants.YES){
							//提示
							/*AjaxUtils.showBkfmConfirm("当前流程已经部署，是否确定需要重新部署？",[{
								text: "重新部署",
								handler: function(self){
									var win = self.ownerCt.ownerCt;
									FlowManger.deployWorkFlow(selfGrid,record.get("WORKFLOW_CODE"));
								}
							},{
								text: "以新流程部署",
								handler: function(self){
									var win = self.ownerCt.ownerCt;
									FlowManager.deployWorkFlow(selfGrid,record.get("WORKFLOW_CODE"));
								}
							},{
								text: "取消",
								handler: function(self){
									var win = self.ownerCt.ownerCt;
									win.close();
								}
							}]);*/
							AjaxUtils.showConfirm(function(){
								FlowManager.deployWorkFlow(selfGrid,record.get("WORKFLOW_CODE"));
							},"当前流程已经部署，是否确定以新流程重新部署？");
						}else{
							FlowManager.deployWorkFlow(selfGrid,record.get("WORKFLOW_CODE"));
						}
					}
				}]
			});
			return grid;
		},
		/**
		 * 部署流程
		 */
		deployWorkFlow: function(grid,workflow_code){
    		AjaxUtils.CallService({
				url: CONTEXT.contextPath+"/wf/v1/deployworkflow",
				params: {
					"workflow_code": workflow_code,
					"deploy_style": "deploy"
				},
				call: function(data){
					if(data.status == "OK"){
						AjaxUtils.showInfo("流程部署成功",function(){
							grid.reload();
						});
					}else{
						AjaxUtils.showError('获取流程信息异常');
					}
				}
			});
		},
		/**
		 * 关闭流程设计器
		 */
		closeFlowDesinger: function(){
			Ext.getCmp("flowWin").close();
		}
}